package com.chenye.rabbitmq.rabbit;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by chenye on 2018/4/15.
 */
@Component
public class HelloSender {
    @Resource(name = "firstRabbitTemplate")
    private AmqpTemplate rabbitTemplate;

    public void send(Integer num) {
        String context = "hello " + new Date()+"  "+num;
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("hello", context);
    }

    public void sendDLX(String queueName,String msg,Long times){
        MessagePostProcessor processor = new MessagePostProcessor(){
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration(times + "");
                message.getMessageProperties().setDelay(1000);
                return message;
            }
        };
        System.out.println("start send dlx message,wait time "+times);
        rabbitTemplate.convertAndSend("direct_ttl_exchange","ttl_routeKey", "tetsetsetset123123123", processor);
    }
}
